﻿using System.Web;
using System.Web.Mvc;
using ECube.Web.CMS.Models;
namespace ECube.Web.CMS.Extensions
{
    public static class UrlHelperExtensions
    {
        public static string SitePageAction(this UrlHelper urlHelper, PageMode mode)
        {
            return urlHelper.SitePageAction(urlHelper.getPageUrl(),mode);

        }
        public static string SitePageAction(this UrlHelper urlHelper,string pageUrl,PageMode mode)
        {
           return urlHelper.Content(pageUrl) + "?" + mode.ToString();
            
        }
        public static string GlobalContentEdit(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("GlobalContentEdit");
        }

        public static string Pages(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("Pages");
        }

        public static string Page(this UrlHelper urlHelper, Page page)
        {
            return urlHelper.Page(page.Slug);
        }

        public static string Page(this UrlHelper urlHelper, string slug)
        {
            return urlHelper.RouteUrl("SitePage", new { pagePath = slug });
        }

        public static string ValidatePageInput(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("ValidatePageInput");
        }

        public static string PageAdd(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("PageAddToSite");
        }

        public static string PageAddContent(this UrlHelper urlHelper, Page page)
        {
            return urlHelper.PageAddContent(page.Slug);
        }

        public static string PageAddContent(this UrlHelper urlHelper, string slug)
        {
            return urlHelper.RouteUrl("PageAddContent", new { pagePath = slug + "/AddContent" });
        }

        public static string PageEdit(this UrlHelper urlHelper, Page page)
        {
            return urlHelper.PageEdit(page.Slug);
        }

        public static string PageEdit(this UrlHelper urlHelper, string slug)
        {
            return urlHelper.RouteUrl("PageEdit", new { pagePath = slug + "/Edit" });
        }

        public static string PageEditContent(this UrlHelper urlHelper, Page page)
        {
            return urlHelper.PageEditContent(page.Slug);
        }

        public static string PageEditContent(this UrlHelper urlHelper, string slug)
        {
            return urlHelper.RouteUrl("PageEditContent", new { pagePath = slug + "/EditContent" });
        }

        public static string PageRemove(this UrlHelper urlHelper, Page page)
        {
            return urlHelper.PageRemove(page.Slug);
        }

        public static string PageRemove(this UrlHelper urlHelper, string slug)
        {
            return urlHelper.RouteUrl("PageRemove", new { pagePath = slug + "/Remove" });
        }

        public static string PageContentAdd(this UrlHelper urlHelper, Page page, string name)
        {
            return urlHelper.PageContentAdd(page.Slug, name);
        }

        public static string PageContentAdd(this UrlHelper urlHelper, string slug, string name)
        {
            return string.Format("{0}?contentName={1}#{2}", urlHelper.RouteUrl("PageEdit", new { pagePath = slug + "/Edit" }), name, "addContentTab");
        }

        public static string PageContentEdit(this UrlHelper urlHelper, Page page, string name)
        {
            return urlHelper.PageContentEdit(page.Slug, name);
        }

        public static string PageContentEdit(this UrlHelper urlHelper, string slug, string name)
        {
            return string.Format("{0}#{1}", urlHelper.RouteUrl("PageEdit", new { pagePath = slug + "/Edit" }), name);
        }

        public static string PageGlobalContentEdit(this UrlHelper urlHelper, string name)
        {
            return urlHelper.RouteUrl("GlobalContentEditAnchor", new { contentItemName = name });
        }

        public static string SiteNavigator(this UrlHelper urlHelper)
        { 
            return urlHelper.Action("Manage","SiteNavigator",new {NewNodeUrl = urlHelper.getPageUrl()});
        }
        public static string SitePageLink(this UrlHelper helper, string linkName, PageMode mode)
        {
            string pagepath = helper.RequestContext.RouteData.Values["pagePath"] as string;
            pagepath = "~/" + pagepath ?? "";
            return string.Format(@"<a href=""{0}"">{1}</a>",
                helper.Content(pagepath) + "?" + mode.ToString(),
                linkName
                );

        }
        public static string SiteMapNodeLink(this UrlHelper helper, MvcSiteMapNode node)
        {
            return string.Format(@"<a href=""{0}"">{1}</a>",helper.Content(node.Url), node.Title);

        }
        public static bool IsCurrentNode(this UrlHelper helper, SiteMapNode node)
        {
            return IsCurrentNode(helper, node, true);
        }

        public static bool IsCurrentNode(this UrlHelper helper, SiteMapNode node,bool isContainDescent)
        {
            HttpContextBase context = helper.RequestContext.HttpContext;
            string rawurl = context.Request.Url.AbsolutePath.ToLower();
            return isCurrentNode(helper, rawurl, node, isContainDescent);
            
        }
        private static string getPageUrl(this UrlHelper urlHelper)
        {
            string pageUrl = urlHelper.RequestContext.RouteData.Values["pagePath"] as string;
            if (string.IsNullOrEmpty(pageUrl))
            {
                pageUrl = "~/";
            }
            return pageUrl;
        }
        private static bool isCurrentNode(this UrlHelper helper,string url, SiteMapNode node, bool isContainDescent)
        {
            if(url == helper.Content(node.Url).ToLower())
            {
                return true;
            }
            else if(isContainDescent)
            {
                foreach(SiteMapNode sn in node.ChildNodes)
                {
                    if(isCurrentNode(helper,url,sn,isContainDescent))
                    {
                        return true;
                    }
                }
            }
            
            return false;
            
        }
    }
}
